layui.config({
    base : "js/"
}).use(['form','layer','jquery','layedit','laydate'],function(){
    var form = layui.form(),
        layer = parent.layer === undefined ? layui.layer : parent.layer,
        laypage = layui.laypage,
        layedit = layui.layedit,
        laydate = layui.laydate,
        $ = layui.jquery;

    var filedir = "drama/"; // 对应oss目录
    var isReUploadCoverFlag = false;
    var delImgsArr = [];

    initInfo(); // 初始化部分信息

    var saveFlag = false;
    // 表单提交:
    form.on("submit(addDrama)",function (data) {
        if(!checkImgUpload()) return false;
        var formData = $("#addDramaForm").serialize();
        $.ajax({
            cache: false,
            type: 'POST',
            url: "/drama/addDrama",
            dataType: "json",
            data: formData,
            async: false,
            success: function(data){
                if(data.result == "success"){
                    top.layer.alert("保存成功",{icon:1});
                    saveFlag = true;
                }else{
                    top.layer.alert("保存异常,请重试",{icon:0});
                }
            },
            error: function(){
                top.layer.alert("发生异常,请重试",{icon:2});
            }
        })

        if(!saveFlag) return false;
    });

    // 保存为草稿
    form.on("submit(addDrama)",function(data){
        form.render();
        if(isReUploadCoverFlag && !checkImgUpload()) return false;

        var formData = $("#addDramaForm").serialize();
        console.log("保存为草稿===="+JSON.stringify(data));
        $.ajax({
            cache: false,
            type: 'POST',
            url: "/drama/updateDrama",
            dataType: "json",
            data: formData,
            async: false,
            success: function(data){
                if(data.result == "success"){
                    top.layer.alert("保存成功",{icon:1});
                    saveFlag = true;
                }else{
                    top.layer.alert("保存异常,请重试",{icon:0});
                }
            },
            error: function(){
                top.layer.alert("发生异常,请重试",{icon:2});
            }
        })

        delImgs(delImgsArr,filedir);
        if(!saveFlag) return false;
    });


    // 点击重新上传封面图()
    $("#reUploadCover").click(function (e) {
        e.preventDefault();
        e.stopPropagation();

        isReUploadCoverFlag = true;

        $("#dramaImgs").hide("100",function(){
            $("#uploadDramaImgWrap").show("200");
        });

        // 删除已上传封面图
        var dramaUrl = $("#dramaUrl").val();
        var urlArr = dramaUrl.split("@@@@");

        for(var i=0,len=urlArr.length;i<len;i++){
            var imgUrl = urlArr[i];
            var imgName = imgUrl.substring(imgUrl.indexOf(filedir),imgUrl.indexOf("?"));//information/20171017142014_psb.jpg
            delImgsArr.push(imgName);
        }
    })

    var imgUrlArr = [];
    initDramaImgUpload();
    // 剧照图上传初始化
    function initDramaImgUpload() {
        $.Tupload.init({
            url: '/oss/ossImgUpload?filedir='+filedir,
            title: "剧照大小不能超过300kb<span style='color:red;'>(*第一张将被作为封面)</span>",
            fileNum: 5, // 上传文件数量
            divId: "T_upload", // div  id
            accept: "image/jpeg,image/x-png,image/jpg", // 上传文件的类型
            fileSize:300,     // 上传文件的大小 kb
            filedir:filedir, // 图片上传至oss对应的目录
            onSuccess: function(data, i) {
                console.log(i+">>>>>>>>"+JSON.stringify(data));
                imgUrlArr[i] = data["link"];
                $("#dramaUrl").val(imgUrlArr.join("@@@@"));
            },
            onDelete: function(i) {

            }
        });
    }

    // 封面图上传是否达5张
    function checkImgUpload(){
        if(imgUrlArr.length < 1){
            top.layer.alert("封面图必须上传一张以上",{icon:0});
            return false;
        }
        return true;
    }

    function delImgs(imgUrlArr,filedir) {
        if(imgUrlArr.length < 1) return false;
        var data = {"imgUrlArr":imgUrlArr,"filedir":filedir};

        $.ajax({
            cache: false,
            type: 'POST',
            url: "/information/delImgs",
            dataType: "json",
            data: data,
            async: false,
            success: function(res){
                if(res.result == "success"){
                    console.log("---------删除成功---------");
                }else{
                    console.log("---------删除异常----------");
                }
            },
            error: function(){
                console.log("发生异常,请重试");
            }
        });
    }
    
    function initInfo() {
        var content = $("#contentH").val();
        document.getElementById("description").value = content;
    }


    var actorsName = [];
    var actorsId = [];
    $("#actors").click(function(){
        var index = layui.layer.open({
            title : "演员选择",
            type : 2,
            content : "../../page/person/actorOpenList.html",
            shadeClose: true,
            closeBtn: 1,
            maxmin: true, //开启最大化最小化按钮
            area: ['860px', '560px'],
            btn: ['确认'],
            yes: function(index, layero){
                var $checkbox = $("#actors_list tbody input[type=\"checkbox\"][name=\"checked\"]", layero.find("iframe")[0].contentWindow.document);
                var $checked = $("#actors_list tbody input[type=\"checkbox\"][name=\"checked\"]:checked", layero.find("iframe")[0].contentWindow.document);

                var objs = [];
                actorsName = [];
                actorsId = [];

                var obj = {};
                for(var j=0;j<$checked.length;j++){
                    obj = $($checked[j]).closest("td").next("td");
                    actorsName.push(obj.text());
                    actorsId.push(obj.attr("actorId"));
                }
                $("#actors").val(actorsName.join(","));
                $("#actorId").val(actorsId.join(","));

                layui.layer.closeAll();
            }
        })
    });
})
